import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    static class Applicant {
        String name;
        int russianScore;
        int mathScore;
        int physicsScore;
        boolean hasOriginalCertificate;

        public Applicant(String name, int russianScore, int mathScore, int physicsScore, boolean hasOriginalCertificate) {
            this.name = name;
            this.russianScore = russianScore;
            this.mathScore = mathScore;
            this.physicsScore = physicsScore;
            this.hasOriginalCertificate = hasOriginalCertificate;
        }

        @Override
        public String toString() {
            return name + "," + russianScore + "," + mathScore + "," + physicsScore + "," + hasOriginalCertificate;
        }
    }

    public static Applicant[] read(String path) {
        List<Applicant> li = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            String line = reader.readLine();
            while (line != null) {
                var t = line.split(",");
                li.add(new Applicant(t[0], Integer.parseInt(t[1]), Integer.parseInt(t[2]), Integer.parseInt(t[3]), Boolean.parseBoolean(t[4])));
                line = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return li.toArray(Applicant[]::new);
    }

    public static void write(Applicant[] arr, String path) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(path));
            for (var i : arr) {
                writer.write(i.toString());
                writer.newLine();
            }
            writer.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        int N = 5; // Количество мест в первой волне

//        Applicant[] applicants = {
//                new Applicant("Иванов Иван Иванович", 5, 4, 4, true),
//                new Applicant("Петров Петр Петрович", 4, 5, 4, true),
//                new Applicant("Сидоров Сидор Сидорович", 3, 4, 5, true),
//                new Applicant("Кузнецова Екатерина Ивановна", 5, 5, 5, false),
//                new Applicant("Михайлова Анна Петровна", 4, 3, 4, true),
//                new Applicant("Смирнов Николай Васильевич", 4, 5, 5, true),
//                new Applicant("Андреев Игорь Александрович", 3, 3, 3, true),
//                new Applicant("Ковалев Артем Владимирович", 5, 5, 5, true),
//                new Applicant("Федорова Анна Александровна", 4, 4, 4, true),
//                new Applicant("Новиков Дмитрий Викторович", 3, 3, 4, true)
//        };
//        write(applicants,"res.txt");
        Applicant[] applicants = read("res.txt");
        List<Applicant> selectedApplicants = new ArrayList<>();
        for (Applicant applicant : applicants) {
            if (applicant.hasOriginalCertificate) {
                selectedApplicants.add(applicant);
            }
        }

        selectedApplicants = selectedApplicants.stream()
                .sorted((a1, a2) -> Integer.compare(a2.russianScore + a2.mathScore + a2.physicsScore, a1.russianScore + a1.mathScore + a1.physicsScore))
                .limit(N)
                .collect(Collectors.toList());

        System.out.println("Количество студентов в первой волне: " + selectedApplicants.size() + "\n");

        for (Applicant applicant : selectedApplicants) {
            System.out.println(applicant.name + ": русский язык - " + applicant.russianScore +
                    ", математика - " + applicant.mathScore + ", физика - " + applicant.physicsScore);
        }
    }
}
